package cn.alcatraz.department.controller;


import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.department.domain.Department;
import cn.alcatraz.department.query.DepartmentQuery;
import cn.alcatraz.department.service.IDepartmentService;
import cn.alcatraz.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/24 20:28
    * @Parameters: []
    * @Return List<Department>
    */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
    * @Title: loadById
    * @Description: 根据ID查询
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/24 20:28
    * @Parameters: [id]
    * @Return Department
    */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Department loadById(@ApiParam(value="部门表的主键",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);
    }
    /**
    * @Title: addOrUpt
    * @Description: 新增或修改
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/24 20:29
    * @Parameters: [department]
    * @Return AjaxResult
    */
    @ApiOperation(value = "通过ID判断新增或修改" )
    @PutMapping
    public AjaxResult addOrUpt(@ApiParam(value="部门对象",required = true) @RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
    /**
    * @Title: delete
    * @Description: 根据ID删除
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/24 20:29
    * @Parameters: [id]
    * @Return AjaxResult
    */
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult delete(@ApiParam(value="部门表的主键",required = true) @PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
    /**
    * @Title: queryData
    * @Description: 查询分页
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/6/24 20:30
    * @Parameters: [departmentQuery]
    * @Return PageList<Department>
    */
    @ApiOperation(value = "查询分页数据" )
    @PostMapping
    public PageList<Department> queryData(@ApiParam(value="分页查询对象",required = true)@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;
    }



}
